# _*_ encoding:utf-8 _*_
# @Time : 2021/6/21 13:23
# @Author : gx

"""
配置帮助文件
"""
import configparser
import json
import os
import sys
import uuid
import config

class BaseConfig(object):
    """
    设置默认值
    """

    def __init__(self, ditction):
        # 项目路径
        # print(config)

        mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
        self.__mac_address = "".join([mac[e:e + 2] for e in range(0, 11, 2)])
        self.__fpath = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + os.path.sep + "..\\..") \
                           .replace("\\", '/') + '/'
        # print(self.__fpath)
        Basedic = {}
        for key, value in ditction.items():
            if len(value) != 0:
                Basedic[key] = value

        self.project_work = self.__fpath

        self.start_time = Basedic['start_time']
        self.end_time = Basedic['end_time']
        self.model_ps = int(Basedic['model_ps'])
        self.model_qs = int(Basedic['model_qs'])
        self.model_rate = float(Basedic['model_rate'])

        self.logging_path = self.__fpath+Basedic['logging_path']





class GetConfig(BaseConfig):
    """
    从配置文件获取配置信息
    """

    def __init__(self):
        self.__conf = configparser.ConfigParser()

        self.__path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/config'
        self.__configpath = self.__path + '/base_config.ini'
        # print(self.__configpath)
        if os.path.exists(self.__configpath) is True:
            # 获取原始配置文件或解码后的配置文件
            self.__conf.read(self.__configpath, encoding='utf-8')
        else:
            print('No such config or directory')
            sys.exit(1)
        dic = {
            'Project_Config': ['start_time', 'end_time', 'model_ps', 'model_qs', 'model_rate'],
            'Logging_Config': ['logging_path']
        }
        BaseConfig.__init__(self, self.__get_config_by_key(dic))

    def __get_config_by_key(self, dic):
        """
        Get the config by key
        :param dic:the key needed
        :return:the config string
        :raises:
            ConfigParser.NoSectionError:no section in config
            ConfigParser.NoOptionError:no option in config
        """
        newdic = {}
        for group, key in dic.items():
            for i in key:
                try:
                    newdic[i] = self.__conf.get(group, i)
                except (configparser.NoSectionError, configparser.NoOptionError):
                    print('No section ' + group + ' or option ' + i + ' in config')
                except AttributeError:
                    print(AttributeError)
                except Exception as e:
                    print('There are errors in config helper')
        return newdic

    def set_config(self, config_ini, section, option, value):
        self.__conf.set(section, option, value)
        self.__conf.write(open(config_ini, 'w+'))


configger = GetConfig()
if __name__ == '__main__':
    print(configger.logging_path)
    print(configger.start_time)
    print(configger.end_time)
    print(configger.model_rate)
